package com.jiada.shiro.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jiada.shiro.test.model.User;
import com.jiada.shiro.test.model.UserExample;
import com.jiada.shiro.test.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	UserService userService;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String loginfail(){
		return "login";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String loginActual(@ModelAttribute("user") @Valid User user,BindingResult result, @ModelAttribute("rememberMe") String rememberMe,
			Model model){
		try{
			Subject subject = SecurityUtils.getSubject();
			// 已登陆则 跳到首页
			if (subject.isAuthenticated()) {
				/*判断登陆用户的类型定位到不同的页面*/
	//			UserExample example = new UserExample();
	//			example.createCriteria().andAccountEqualTo(user.getAccount());
	//			User authUserInfo = userService.selectByExample(example).get(0);
	//			if(authUserInfo.getType() == EnumUserType.NORMAL){
	//				return "redirect:/rest/myClassCourse/list";
	//			}
				return "redirect:/rest/index";
			}
			if (result.hasErrors()) {
				// model.addAttribute("error", "参数错误！");
				return "login";
			}
			// 身份验证
			UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
			
			subject.login(token);
			
			// 验证成功在Session中保存用户信息
			UserExample example = new UserExample();
			example.createCriteria().andUsernameNotEqualTo(user.getUsername());
			User authUserInfo = userService.selectByExample(example).get(0);
			SecurityUtils.getSubject().getSession().setAttribute("loginUser", authUserInfo);
			} 
		catch (AuthenticationException e) {
			// 身份验证失败
			model.addAttribute("error", "用户名密码错误！");
			return "login";
		}
		/*判断登陆用户的类型定位到不同的页面*/
	//	UserExample example = new UserExample();
	//	example.createCriteria().andAccountEqualTo(user.getAccount());
	//	User authUserInfo = userService.selectByExample(example).get(0);
	//	if(authUserInfo.getType() == EnumUserType.NORMAL){
	//		return "redirect:/rest/myClassCourse/list";
	//	}
		return "redirect:/rest/index";
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(HttpSession session){
		session.removeAttribute("userInfo");
		// 登出操作
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "redirect:/rest/user/login";
	}
	
	@RequiresPermissions("test:user:view")
	@RequestMapping("/list")
	public String list(Model model){
		UserExample example = new UserExample();
		List<User> userList = userService.selectByExample(example);
		model.addAttribute("userlist", userList);
		return "user/userlist";
	}
	
	@RequiresPermissions("test:book:view")
	@RequestMapping("/form")
	public String form(User user,Model model){
		User target = userService.selectById(user);
		model.addAttribute("user",target);
		return "user/userForm";
	}
	
	@RequiresPermissions("test:book:edit")
	@RequestMapping("/save")
	public String save(User user){
		userService.saveBook(user);
		return "redirect:/rest/user/list";
	}
	
	@RequiresPermissions("test:book:edit")
	@RequestMapping("/delete")
	public String delete(User user){
		userService.deleteBook(user);
		return "redirect:/rest/user/list";
	}
	
}
